package com.ac.cv;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class TestableBean {

	private Object object;

	private List<Method> getters;

	public List<Method> getGetters() {
		return getters;
	}

	public void setGetters(List<Method> getters) {
		this.getters = getters;
	}

	public TestableBean(Object object) {
		this.object = object;
		getters = new ArrayList<Method>();
		for (Method m : object.getClass().getMethods()) {
			if (m.getName().startsWith("get") && !m.getName().equals("getClass")) {
				getters.add(m);
			}
		}
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public boolean contains(Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		for (Method m : getters) {
			if (value == null) {
				if (m.invoke(object) == null) {
					return true;
				}
			} else {
				if (m.invoke(object) != null && value.toString().equals(m.invoke(object).toString())) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean contains(TestableBean bean) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		for (Method m : bean.getGetters()) {
			if (!this.contains(m.invoke(bean.getObject()))) {
				return false;
			}
		}
		return true;
	}


}